package ljj.rollcall;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


/**
 * 实现 根据输入内容查询数据库并将查询结果传递给activity_show_rollcall
 */
public class RollCall_Activity extends AppCompatActivity {

    private RollCall_OpenHelper dbHelper;
    private SQLiteDatabase db;
    private Button confirm;
    private String class_name;
    private Cursor cursor;
    @Override
    protected  void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rollcall);
        //获取Intent
        Intent gIntent=getIntent();
        //从获取到的Itent传递的参数
        String txtID=gIntent.getStringExtra("uid");
        //实例化dbHelper
        dbHelper=new RollCall_OpenHelper(this, RollCall_OpenHelper.ROLLCALL,null,1);
        //获取数据库并读写方式打开
        db=dbHelper.getWritableDatabase();
        cursor=db.rawQuery("select _id , cname from teachinfo where teachinfo.uname = ( select userinfo.uname from userinfo where userinfo.uid = ? )",new String[]{txtID});
        //获取 输入班级 编辑文本
        final EditText inputClass =(EditText)findViewById(R.id.classname);
        //获取 确定 按钮
        confirm =(Button)findViewById(R.id.startRollCall);
        //确定 按钮 的 监听事件
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取 班级名
                class_name= inputClass.getText().toString().trim();
                //创建Intent对象 并实例化
                Intent intent = new Intent(RollCall_Activity.this, Show_RollCall_Activity.class);
                //传递参数 班级名
                intent.putExtra("class",class_name);
                //调用getMaxRow获取最大记录行数
                getMaxRow();
                //创建Bundle对象 并实例化
                Bundle bundle=new Bundle();
                //将getMaxRow封装到bunle
                bundle.putInt("MaxRow",getMaxRow());
                //传递bundle
                intent.putExtras(bundle);
                //关闭游标
                cursor.close();
                //启动activity 跳转到activity_show_rollcall
                startActivity(intent);
            }
        });
    }
    //获取最大记录行数方法
    private int getMaxRow() {
        //查询数据返回 cursor对象
        Cursor cursor = db.rawQuery("select _id, sid, sname from studentinfo where studentinfo.cname = ?", new String[]{class_name});
        //获取记录最大行数
        int MaxRow=cursor.getCount();
        //返回最大行数
        return MaxRow ;
    }
}
